﻿using HtmlAgilityPack;
using Ivony.Html;
using Ivony.Html.Parser;
using NetCore.Novels.Consoles.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NetCore.Novels.Consoles
{
    public static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            {
                var novelUrl = "https://www.hgq26.com/162/162913/55929295.html";
                novelUrl = "https://www.yeyezw.com/read/20814/886605.html";
                var contentLabel = "#content";
                var encoding = "gbk";
                encoding = "gb2312";
                encoding = "utf-8";
                // 在使用编码方法（Encoding.GetEncoding("GB2312")）之前，对编码进行注册（ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);）
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Encoding defaultEncoding = System.Text.Encoding.GetEncoding(encoding);

                {
                    IHtmlDocument html = new JumonyParser().LoadDocument(novelUrl, defaultEncoding);
                    var abc = html;
                    var ht = html.RawHtml?.ToString();
                    var dd = ht;
                    try
                    {
                        var content = "";

                        if (contentLabel.IndexOf(" p", StringComparison.Ordinal) > -1
                            || contentLabel.IndexOf(" li", StringComparison.Ordinal) > -1)
                        {
                            var contents = html.Find(contentLabel).ToList();
                            if (contents.Count > 0)
                            {
                                foreach (var element in contents)
                                {
                                    content += element.InnerText() + Environment.NewLine;
                                }
                            }
                        }
                        else
                        {
                            content = html.Find(contentLabel).FirstOrDefault().InnerHtml();
                        }

                        Console.WriteLine($"{content}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{ex}");
                    }

                }

                {
                    // HtmlAgilityPack
                    // https://html-agility-pack.net/
                    // https://html-agility-pack.net/online-examples
                    // https://www.w3school.com.cn/xpath/xpath_syntax.asp

                    #region 示例代码

                    //// From File
                    //var doc = new HtmlDocument();
                    //doc.Load(filePath);

                    //// From String
                    //var doc = new HtmlDocument();
                    //doc.LoadHtml(html);

                    //// From Web
                    //var url = "http://html-agility-pack.net/";
                    //var web = new HtmlWeb();
                    //var doc = web.Load(url);

                    #endregion

                    //var html = HttpHelper.HttpGet(new Uri(novelUrl));
                    //Console.WriteLine($"{html}");
                    var xpath = "//div[@id='content']/p";
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    HtmlWeb web = new HtmlWeb();
                    var doc = web.LoadFromWebAsync(novelUrl).Result;
                    var nodes = doc.DocumentNode.SelectNodes(xpath);

                    foreach (var item in nodes)
                    {
                        //Console.WriteLine($"标题：{item.InnerText} 地址：{item.Attributes["href"].Value}");
                        Console.WriteLine($"{item.InnerText}");
                    }

                    //IHtmlParser parser = new();

                    //var document = parser.Parse(html, uri);
                }

                {
                    var html =
                        @"<TD class=texte width=""50%"">
			                <DIV align=right>Name :<B> </B></DIV>
		                </TD>
		                <TD width=""50%"">
    		                <INPUT class=box value=John maxLength=16 size=16 name=user_name>
    		                <INPUT class=box value=Tony maxLength=16 size=16 name=user_name>
    		                <INPUT class=box value=Jams maxLength=16 size=16 name=user_name>
		                </TD>
		                <TR vAlign=center>";

                    var htmlDoc = new HtmlDocument();
                    htmlDoc.LoadHtml(html);

                    string name = htmlDoc.DocumentNode
                                    .SelectNodes("//td/input")
                                    .First()
                                    .Attributes["value"].Value;

                    Console.WriteLine(name);
                }

                {
                    ParseHtml(html1, xpath1);
                    Console.WriteLine("---------------");
                    ParseHtml(html2, xpath2);
                }

                {
                    var website = new Website { Id = 1, Name = "Test", Url = "https://bleach.forumpolish.com/", Desc = "" };
                    var source = new SourceThread
                    {
                        Id = 1,
                        Type = "mini",
                        Url = "https://bleach.forumpolish.com/t84-zmiany",
                        Travel = "//td[contains(@class, 'pagination')]//a[last()-1]",
                        PostContainer = "//tr[contains(@class, 'post post')]",
                        PostBody = ".//div[contains(@class, 'postbody')]",
                        PostDate = ".//span[contains(@class, 'lastpostText')]",
                        DateCulture = "pl-PL",
                        Skip = 0
                    };
                    var sources = new List<SourceThread>() { source };

                    CollectThreadNews(website, sources).Wait();
                }
            }

            {
                var htmlDocument = new HtmlDocument();
                var divs = htmlDocument.DocumentNode.Descendants("div")
                    .Where(node => node.GetAttributeValue("class", "")
                           .Equals("w-1/2 px-1 mb-2 md:px-2 md:mb-4 md:w-1/4")).ToList();

                var produse = new List<Produs>();

                Console.WriteLine("before loop");
                foreach (var div in divs)
                {
                    var produs = new Produs
                    {
                        Nume = div?.Descendants("a")?.FirstOrDefault()?
                            .ChildAttributes("title")?.FirstOrDefault()?.Value,
                        Pret = div?.Descendants("span")?.FirstOrDefault()?.InnerText,

                    };
                }
                Console.WriteLine("complete.");
            }

            {
                var uriString = "https://www.mtggoldfish.com/index/modern#online";
                WebClient myWebClient = new WebClient();
                // Download home page data. 
                Console.WriteLine("Accessing {0} ...", uriString);
                // Open a stream to point to the data stream coming from the Web resource.
                Stream myStream = myWebClient.OpenRead(uriString);

                Console.WriteLine("\nDisplaying Data :\n");
                StreamReader stream = new StreamReader(myStream);
                var allData = stream.ReadToEnd();

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

                // There are various options, set as needed
                htmlDoc.OptionFixNestedTags = true;

                htmlDoc.LoadHtml(allData);  //to load from a string (was htmlDoc.LoadXML(xmlString)

                if (htmlDoc.DocumentNode != null)
                {
                    var rows = htmlDoc.DocumentNode.SelectNodes("//div[@class='index-price-table-online']//tbody//tr");

                    foreach (var row in rows)
                    {
                        var name = HtmlEntity.DeEntitize(row.SelectSingleNode(".//span[@class='card_name']/a").InnerText.Trim());
                        var price = row.SelectSingleNode("./td[4]").InnerText.Trim();

                        Console.WriteLine(name + ": " + price);
                    }
                }
            }

            {

            }

            Console.ReadKey();
        }





        public static void ParseHtml(string html, string xpath)
        {
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(html);


            var nodes = htmlDoc.DocumentNode
                            .SelectNodes(xpath);
            if (nodes != null)
                foreach (var node in nodes)
                {
                    var src = node.Attributes["src"].Value;
                    var alt = node.Attributes["alt"].Value;
                    Console.WriteLine($"src={src}, alt={alt}");
                }
        }

        #region testData

        public static string xpath1 = "//div[@id='publication-cover']/img";
        public static string html1 = @"<html class=""NEWSSTAND"" lang=""ru-RU"" style=""--vh:6.35px;"">
	<head>
		<title>Dwell — подписка</title>
		<meta name=""description"" content=""Все номера «Dwell» в нашем магазине. Оформите подписку на «Dwell» и получите цифровой журнал на своем устройстве."">
		<meta charset=""UTF-8""><meta name=""viewport"" content=""width=device-width, initial-scale=1, maximum-scale=3.0, user-scalable=0"">
		<meta name=""apple-itunes-app"" content=""app-id=364297166, app-argument=https://www.zinio.com?uid=364297166&amp;amp;source=smartAppBanner"">
	</head>
	<body data-new-gr-c-s-check-loaded=""14.1027.0"" data-gr-ext-installed="""">
		<div class=""ProductLayout-id_1hbuy9v cBitLf"">
		<div id=""publication-cover"" class=""ProductCoverStyled-id_1nyj96g dHQEZu"">
			<img src=""https://cdn-assets.ziniopro.com/var/site_731/storage/images/media2/cover248/255026-1-eng-US/cover2.jpg?h=675"" alt="""" class=""AsyncImageSC-id_kscvy9 jHxmWp"">
		</div>
		<div class=""ProductInfo-id_1hbuy9v jWzcJw""><h1 class=""H1Typography-id_kjd8qk gbwQDL"">Dwell </h1><span class=""H4Typography-id_kjd8qk cFMiNO"">July/August 2021</span><button class=""FollowPublicationIconButton-id_1hbuy9v gwaDhk IconButtonSC-id_4gk3cg jnMLWS"" aria-disabled=""false""><div size=""24"" class=""StyledIcon-id_4gk3cg jTxYTM""><svg viewBox=""0 0 24 24""><path d=""M16.5 3c-1.74 0-3.41.81-4.5 2.09C10.91 3.81 9.24 3 7.5 3 4.42 3 2 5.42 2 8.5c0 3.78 3.4 6.86 8.55 11.54L12 21.35l1.45-1.32C18.6 15.36 22 12.28 22 8.5 22 5.42 19.58 3 16.5 3zm-4.4 15.55l-.1.1-.1-.1C7.14 14.24 4 11.39 4 8.5 4 6.5 5.5 5 7.5 5c1.54 0 3.04.99 3.57 2.36h1.87C13.46 5.99 14.96 5 16.5 5c2 0 3.5 1.5 3.5 3.5 0 2.89-3.14 5.74-7.9 10.05z""></path></svg><span class=""H4Typography-id_kjd8qk cFMiNO"">Добавить в Избранное</span></div></button><div class=""ProductDescriptionStyled-id_kmwg26 bkZFYR""><p class=""description Body1Typography-id_1pbrku5 hrhCkZ"">Dwell is the unique modern architecture and design magazine for people who believe that good design is an integral part of real life. Get Dwell digital magazine subscription today.</p><div class=""ToggleDetails-id_kmwg26 hrnDIq""><span class=""Body1Typography-id_1pbrku5 enKqQI"">Читать больше</span><div class=""StyledIcon-id_4gk3cg guPVEq"" size=""16"" role=""presentation""><svg viewBox=""0 0 24 24""><path d=""M7.41 7.84L12 12.42l4.59-4.58L18 9.25l-6 6-6-6z""></path></svg></div></div><div class=""ProductDetails-id_kmwg26 dFgRIA""><div class=""Table-id_kmwg26 gghRzL""><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""H4Typography-id_kjd8qk cFMiNO"">Страна<!-- -->:</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""SubtitleTypography-id_dzsvkh hbwRXX"">United States</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""H4Typography-id_kjd8qk cFMiNO"">Язык<!-- -->:</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""SubtitleTypography-id_dzsvkh hbwRXX"">English</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""H4Typography-id_kjd8qk cFMiNO"">Издатель<!-- -->:</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""SubtitleTypography-id_dzsvkh hbwRXX"">Dwell</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""H4Typography-id_kjd8qk cFMiNO"">Периодичность<!-- -->:</span></div><div class=""TableCell-id_kmwg26 cvjXZU""><span class=""SubtitleTypography-id_dzsvkh hbwRXX"">Monthly</span></div></div></div></div><div class=""ProductActions-id_1hbuy9v dfjjWS""><div class=""ProductButtons-id_1hbuy9v lgYDCF""><div id=""issue-buy-section""><button id=""issue-buy-btn"" class=""StyledButton-id_136n8tf jrzovu"">КУПИТЬ ВЫПУСК</button><div class=""PricingTable-id_1hbuy9v dVZSpC""><span class=""Price-id_1hbuy9v fQLdvB"">656,63&nbsp;₽</span><span class=""Taxes-id_1hbuy9v eRFmqO""></span></div></div><div id=""issue-subscribe-section""><button id=""issue-subscribe-btn"" class=""StyledButton-id_136n8tf fEGYxr"">ПОДПИСАТЬСЯ</button><div class=""PricingTable-id_1hbuy9v dVZSpC""><span class=""Price-id_1hbuy9v fQLdvB"">1&nbsp;460,06&nbsp;₽</span><span class=""Taxes-id_1hbuy9v eRFmqO""></span></div><span class=""TotalIssues-id_1hbuy9v bikCcN"">6 Выпуск(ов)</span></div></div></div></div></div>
	</body>";

        public static string xpath2 = "//article[@class='issue ']/a/img";
        public static string html2 = @" < html class=""NEWSSTAND"" lang=""ru-RU"" style=""--vh:6.35px;"">
	<head>
		<title>Dwell — подписка</title>
		<meta name=""description"" content=""Все номера «Dwell» в нашем магазине. Оформите подписку на «Dwell» и получите цифровой журнал на своем устройстве."">
		<meta charset=""UTF-8""><meta name=""viewport"" content=""width=device-width, initial-scale=1, maximum-scale=3.0, user-scalable=0"">
		<meta name=""apple-itunes-app"" content=""app-id=364297166, app-argument=https://www.zinio.com?uid=364297166&amp;amp;source=smartAppBanner"">
	</head>
	<body data-new-gr-c-s-check-loaded=""14.1027.0"" data-gr-ext-installed="""">
		<div class=""container page-wrapper"">
<div class=""archive__header"">
<h1>
<span class=""archive__title"">D Magazine</span>
<span class=""archive__subtitle"">2021</span>
</h1>
<a href=""https://subscribe.dmagazine.com/ecom/DMG/app/live/subscriptions?org=DMG&amp;publ=DM&amp;key_code=I14MW&amp;type=S&amp;gift_key=G14MW"" target=""_blank"" class=""button--blue button--small archive__button"">Subscribe to D Magazine</a>
</div>
<section class=""issues"">
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/january/"">
<img alt=""January 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/01/Cover_Jan21-685x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/01/Cover_Jan21-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/01/Cover_Jan21-scaled.jpg"">
<h1 class=""issue__title "">
January
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine January 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-january-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/february/"">
<img alt=""February 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/01/D_Cover_Feb21-684x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/01/D_Cover_Feb21-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/01/D_Cover_Feb21.jpg"">
<h1 class=""issue__title "">
February
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine February 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-february-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/march/"">
<img alt=""March 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/02/Cover_Mar21-685x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/02/Cover_Mar21-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/02/Cover_Mar21-scaled.jpg"">
<h1 class=""issue__title "">
March
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine March 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-march-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/april/"">
<img alt=""April 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/03/D-Magazine-Cover_April-2021-684x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/03/D-Magazine-Cover_April-2021-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/03/D-Magazine-Cover_April-2021.jpg"">
<h1 class=""issue__title "">
April
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine April 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-april-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/may/"">
<img alt=""May 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/04/Cover_May21-685x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/04/Cover_May21-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/04/Cover_May21-scaled.jpg"">
<h1 class=""issue__title "">
May
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine May 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-may-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/june/"">
<img alt=""June 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/06/DMag_Cover_June21-684x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/06/DMag_Cover_June21-394x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/06/DMag_Cover_June21.jpg"">
<h1 class=""issue__title "">
June
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine June 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-june-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/july/"">
<img alt=""July 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/06/D-magazine-cover-july-2021-685x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/06/D-magazine-cover-july-2021-395x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/06/D-magazine-cover-july-2021.jpg"">
<h1 class=""issue__title "">
July
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine July 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-july-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
<article class=""issue "">
<a href=""https://www.dmagazine.com/publications/d-magazine/2021/august/"">
<img alt=""August 2021 cover"" class=""image"" srcset=""https://assets.dmagstatic.com/wp-content/uploads/2021/07/D-magazine-Cover-August-2021-685x920.jpg 1200w, https://assets.dmagstatic.com/wp-content/uploads/2021/07/D-magazine-Cover-August-2021-395x530.jpg 677w"" sizes=""(max-width: 1024px) 33vw, 188px"" src=""https://assets.dmagstatic.com/wp-content/uploads/2021/07/D-magazine-Cover-August-2021.jpg"">
<h1 class=""issue__title "">
August
2021
</h1>
</a>
<form target=""paypal"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"">
<input type=""hidden"" name=""cmd"" value=""_cart"">
<input type=""hidden"" name=""business"" value=""specialorders@dmagazine.com"">
<input type=""hidden"" name=""add"" value=""1"">
<input type=""hidden"" name=""item_name"" value=""D Magazine August 2021 Issue"">
<input type=""hidden"" name=""item_number"" value=""d-magazine-august-2021"">
<input type=""hidden"" name=""handling_cart"" value=""6.5"">
<input type=""hidden"" name=""shipping"" value=""1"">
<input type=""hidden"" name=""shipping2"" value=""1"">
<input type=""hidden"" name=""amount"" value=""5.99"">
<input class=""button--small button--outline--blue button--full"" type=""submit"" name=""submit"" value=""Buy"">
</form>
</article>
</section>
</div>
	</body>
</html>";
        #endregion






        #region news net5

        public class Website
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Url { get; set; }
            public string Desc { get; set; }
        }

        public class SourceThread
        {
            public int Id { get; set; }
            public string Type { get; set; }
            public string Url { get; set; }
            public string Travel { get; set; }
            public string PostContainer { get; set; }
            public int Skip { get; set; }
            public string PostBody { get; set; }
            public string PostDate { get; set; }
            public string DateCulture { get; set; }
            public string DateFormat { get; set; }
        }

        public static JsonElement Travel(this JsonElement element, string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return element;
            }
            string[] properties = path.Split('/');
            foreach (var property in properties)
            {
                element = element.GetProperty(property);
            }
            return element;
        }

        public static string ClearHTML(string message)
        {
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(message);
            return document.DocumentNode.InnerText;
        }

        public static async Task CollectThreadNews(Website website, List<SourceThread> sources)
        {
            var limit = 7;
            foreach (var source in sources)
            {
                var count = 0;
                try
                {
                    var web = new HtmlWeb();
                    var doc = await web.LoadFromWebAsync(source.Url);
                    if (source.Travel != null)
                    {
                        var travelUrl = doc.DocumentNode.SelectSingleNode(source.Travel)?.Attributes["href"].Value;
                        Console.WriteLine(travelUrl);
                        if (travelUrl != null)
                        {
                            travelUrl = travelUrl.StartsWith("http") ? travelUrl : website.Url + travelUrl;
                            travelUrl = travelUrl.Replace("amp;", "");
                            doc = await web.LoadFromWebAsync(travelUrl);
                        }
                    }
                    var title = doc.DocumentNode.SelectSingleNode("//head/title")?.InnerText;
                    var nodes = doc.DocumentNode.SelectNodes(source.PostContainer).Skip(source.Skip).Reverse();
                    foreach (var node in nodes)
                    {
                        //Console.WriteLine(node.OuterHtml);
                        count++;
                        if (count >= limit)
                            break;
                        try
                        {
                            var post = node.SelectSingleNode(source.PostBody)?.InnerHtml;
                            var date = node.SelectSingleNode(source.PostDate)?.InnerText;
                            Console.WriteLine(date);
                            if (post != null && date != null && title != null)
                            {
                                date = Regex.Replace(date, "(dzisiaj|today|wczoraj|yesterday),?", "");
                                bool parseSuccess = false;
                                DateTime newDate = DateTime.Now;
                                if (source.DateCulture != null)
                                    parseSuccess = DateTime.TryParse(date, new CultureInfo(source.DateCulture), DateTimeStyles.None, out newDate);
                                else if (source.DateFormat != null)
                                    parseSuccess = DateTime.TryParseExact(date, source.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out newDate);
                                var news = new
                                {
                                    Title = title.Trim(),
                                    Text = post.Trim(),
                                    Date = newDate,
                                    Link = source.Url,
                                    Website = website,
                                    Type = source.Type
                                };
                                Console.WriteLine(news.Date);
                                Console.WriteLine(news.Text);

                            }
                        }
                        catch (Exception e2) { Console.WriteLine(e2.StackTrace); }
                    }
                }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
        }

        #endregion

    }

    public class Produs
    {
        public string Nume { get; set; }

        public string Pret { get; set; }

    }
}
